<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
    	// Return the notice information
		function getNotification() {
			$db = new Connect();
			
			$sql = "SELECT subject, pName, Posting.pID, nDatetime, body " .
				   "FROM Notice " .
				   "INNER JOIN Posting ON Posting.pID = Notice.pID " .
				   "WHERE nID = " . $_GET['nid'];
				   
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find notice
			{
				return;
			}
			else // Return notice info
			{
				$rows = $db->to_array($result);
				return $rows[0];
			}
		}
		
		// Mark the notice as read
		function readNotification() {
			$db = new Connect();
			
			$sql = "UPDATE Notice " .
				   "SET isRead = 1 " .
				   "WHERE nID = " . $_GET['nid'];
				   
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Return notice read failure
			{
				return false;
			}
			else // Return notice read success
			{
				return true;
			}
		}
    ?>
    <div id="main">
		<p class="title">
			Message
		</p>
<?php
	$notification = getNotification();
	readNotification();
	
	if (isset($notification))
	{
		print "<p class=\"normal\"><span class=\"bold\">Subject</span>: " . $notification['subject'] . "</p>";
		print "<p class=\"normal\"><span class=\"bold\">Origin</span>: <a href=\"posting.php?pid=" . $notification['pID'] . "\">" . $notification['pName'] . "</a></p>";
		print "<p class=\"normal\"><span class=\"bold\">Date</span>: " . $notification['nDatetime'] . "</p>";
		print "<hr/>";
		print "<p class=\"normal\">" . $notification['body'] . "</p>";
	}
	else
	{
		print "<p></p><span class=\"error\">INVALID MESSAGE SPECIFIED!</span>";
	}
?>
        </form>
    </div>
</body>
</html>
